home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_313 / uucp / uucp1.lzh / src / lib / serialport.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  3KB  |  111 lines

  1.  
  2. /*
  3.  *  SERIALPORT.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  6.  *
  7.  *  Serial Port locking to prevent collisions between, say, a Getty
  8.  *  accepting a login sequence and a uucico calling up another machine.
  9.  *
  10.  *  The existance of the public port indicates a lock.    People waiting
  11.  *  for the lock PutMsg() EXEC messages to the port.  Upon unlocking,
  12.  *  the unlocker will deallocate the port only if no messages are
  13.  *  pending, else it will ReplyMsg() the first message in the queue
  14.  *  and NOT deallocate the port.
  15.  *
  16.  *  On receiving a message back you own the port and its memory
  17.  */
  18.  
  19. #include <proto/all.h>
  20. #include <exec/memory.h>
  21.  
  22. typedef struct MsgPort    PORT;
  23. typedef struct Message    MSG;
  24.  
  25. struct SMsgPort {
  26.     PORT    Port;
  27.     short   NameLen;
  28.     short   Reserved;
  29. };
  30.  
  31. typedef struct SMsgPort SPORT;
  32.  
  33. static SPORT    *SPLock;
  34.  
  35. int IAmGetty = 0;
  36.  
  37. void
  38. LockSerialPort(name, unit)
  39. char *name;
  40. long unit;
  41. {
  42.     short namelen = strlen(name) + 32;
  43.     char *portname;
  44.     SPORT *sport;
  45.     PORT  *rport = NULL;
  46.     MSG   msg;
  47.  
  48.     if (SPLock)
  49.     return;
  50.  
  51.     portname = AllocMem(namelen, MEMF_PUBLIC | MEMF_CLEAR);
  52.  
  53.     sprintf(portname, "SPLock-%d-%s", unit, name);
  54.  
  55.     Forbid();
  56.     if (sport = (SPORT *)FindPort(portname)) {
  57.     rport = CreatePort(NULL, 0);
  58.     msg.mn_ReplyPort = rport;
  59.     msg.mn_Length = 0;
  60.     if (IAmGetty)
  61.         AddHead(&sport->Port.mp_MsgList, &msg);
  62.     else
  63.         AddTail(&sport->Port.mp_MsgList, &msg);
  64.     FreeMem(portname, namelen);
  65.     } else {
  66.     sport = AllocMem(sizeof(SPORT), MEMF_PUBLIC | MEMF_CLEAR);
  67.     sport->Port.mp_Node.ln_Name = portname;
  68.     sport->Port.mp_Node.ln_Type = NT_MSGPORT;
  69.     sport->Port.mp_Flags = PA_IGNORE;
  70.     sport->NameLen = namelen;
  71.     AddPort(&sport->Port);
  72.     }
  73.     Permit();
  74.     SPLock = sport;
  75.     if (rport) {            /*  wait for message to be returned */
  76.     WaitPort(rport);
  77.     DeletePort(rport);
  78.     }
  79. }
  80.  
  81. /*
  82.  *  Unlock the serial port.  If I am NOT the Getty then delay before
  83.  *  unlocking to give the Getty a chance to take the next lock (it takes
  84.  *  about a second for the Getty to realize the serial.device has been
  85.  *  closed and try to take the lock.
  86.  */
  87.  
  88. void
  89. UnLockSerialPort(name, unit)
  90. char *name;
  91. long unit;
  92. {
  93.     MSG *msg;
  94.  
  95.     if (SPLock) {
  96.     if (IAmGetty == 0)
  97.         sleep(2);
  98.     Forbid();
  99.     if (msg = GetMsg(&SPLock->Port)) {  /*  somebody else wants it */
  100.         ReplyMsg(msg);
  101.     } else {            /*  nobody else wants it   */
  102.         RemPort(&SPLock->Port);
  103.         FreeMem(SPLock->Port.mp_Node.ln_Name, SPLock->NameLen);
  104.         FreeMem(SPLock, sizeof(SPORT));
  105.     }
  106.     Permit();
  107.     SPLock = NULL;
  108.     }
  109. }
  110.  
  111.